1 /* 2 * This file is part of gtkD. 3 * 4 * gtkD is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU Lesser General Public License 6 * as published by the Free Software Foundation; either version 3 7 * of the License, or (at your option) any later version, with 8 * some exceptions, please read the COPYING file. 9 * 10 * gtkD is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU Lesser General Public License for more details. 14 * 15 * You should have received a copy of the GNU Lesser General Public License 16 * along with gtkD; if not, write to the Free Software 17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA 18 */ 19 20 // generated automatically - do not change 21 // find conversion definition on APILookup.txt 22 // implement new conversion functionalities on the wrap.utils pakage 23 24 25 module gio.Task; 26 27 private import gio.AsyncResultIF; 28 private import gio.AsyncResultT; 29 private import gio.Cancellable; 30 private import gio.c.functions; 31 public import gio.c.types; 32 private import glib.ConstructionException; 33 private import glib.ErrorG; 34 private import glib.GException; 35 private import glib.MainContext; 36 private import glib.MemorySlice; 37 private import glib.Source; 38 private import glib.Str; 39 private import glib.c.functions; 40 private import gobject.ObjectG; 41 private import gobject.Value; 42 43 44 /** 45 * A #GTask represents and manages a cancellable "task". 46 * 47 * ## Asynchronous operations 48 * 49 * The most common usage of #GTask is as a #GAsyncResult, to 50 * manage data during an asynchronous operation. You call 51 * g_task_new() in the "start" method, followed by 52 * g_task_set_task_data() and the like if you need to keep some 53 * additional data associated with the task, and then pass the 54 * task object around through your asynchronous operation. 55 * Eventually, you will call a method such as 56 * g_task_return_pointer() or g_task_return_error(), which will 57 * save the value you give it and then invoke the task's callback 58 * function in the 59 * [thread-default main context][g-main-context-push-thread-default] 60 * where it was created (waiting until the next iteration of the main 61 * loop first, if necessary). The caller will pass the #GTask back to 62 * the operation's finish function (as a #GAsyncResult), and you can 63 * use g_task_propagate_pointer() or the like to extract the 64 * return value. 65 * 66 * Using #GTask requires the thread-default #GMainContext from when the 67 * #GTask was constructed to be running at least until the task has completed 68 * and its data has been freed. 69 * 70 * Here is an example for using GTask as a GAsyncResult: 71 * |[<!-- language="C" --> 72 * typedef struct { 73 * CakeFrostingType frosting; 74 * char *message; 75 * } DecorationData; 76 * 77 * static void 78 * decoration_data_free (DecorationData *decoration) 79 * { 80 * g_free (decoration->message); 81 * g_slice_free (DecorationData, decoration); 82 * } 83 * 84 * static void 85 * baked_cb (Cake *cake, 86 * gpointer user_data) 87 * { 88 * GTask *task = user_data; 89 * DecorationData *decoration = g_task_get_task_data (task); 90 * GError *error = NULL; 91 * 92 * if (cake == NULL) 93 * { 94 * g_task_return_new_error (task, BAKER_ERROR, BAKER_ERROR_NO_FLOUR, 95 * "Go to the supermarket"); 96 * g_object_unref (task); 97 * return; 98 * } 99 * 100 * if (!cake_decorate (cake, decoration->frosting, decoration->message, &error)) 101 * { 102 * g_object_unref (cake); 103 * // g_task_return_error() takes ownership of error 104 * g_task_return_error (task, error); 105 * g_object_unref (task); 106 * return; 107 * } 108 * 109 * g_task_return_pointer (task, cake, g_object_unref); 110 * g_object_unref (task); 111 * } 112 * 113 * void 114 * baker_bake_cake_async (Baker *self, 115 * guint radius, 116 * CakeFlavor flavor, 117 * CakeFrostingType frosting, 118 * const char *message, 119 * GCancellable *cancellable, 120 * GAsyncReadyCallback callback, 121 * gpointer user_data) 122 * { 123 * GTask *task; 124 * DecorationData *decoration; 125 * Cake *cake; 126 * 127 * task = g_task_new (self, cancellable, callback, user_data); 128 * if (radius < 3) 129 * { 130 * g_task_return_new_error (task, BAKER_ERROR, BAKER_ERROR_TOO_SMALL, 131 * "%ucm radius cakes are silly", 132 * radius); 133 * g_object_unref (task); 134 * return; 135 * } 136 * 137 * cake = _baker_get_cached_cake (self, radius, flavor, frosting, message); 138 * if (cake != NULL) 139 * { 140 * // _baker_get_cached_cake() returns a reffed cake 141 * g_task_return_pointer (task, cake, g_object_unref); 142 * g_object_unref (task); 143 * return; 144 * } 145 * 146 * decoration = g_slice_new (DecorationData); 147 * decoration->frosting = frosting; 148 * decoration->message = g_strdup (message); 149 * g_task_set_task_data (task, decoration, (GDestroyNotify) decoration_data_free); 150 * 151 * _baker_begin_cake (self, radius, flavor, cancellable, baked_cb, task); 152 * } 153 * 154 * Cake * 155 * baker_bake_cake_finish (Baker *self, 156 * GAsyncResult *result, 157 * GError **error) 158 * { 159 * g_return_val_if_fail (g_task_is_valid (result, self), NULL); 160 * 161 * return g_task_propagate_pointer (G_TASK (result), error); 162 * } 163 * ]| 164 * 165 * ## Chained asynchronous operations 166 * 167 * #GTask also tries to simplify asynchronous operations that 168 * internally chain together several smaller asynchronous 169 * operations. g_task_get_cancellable(), g_task_get_context(), 170 * and g_task_get_priority() allow you to get back the task's 171 * #GCancellable, #GMainContext, and [I/O priority][io-priority] 172 * when starting a new subtask, so you don't have to keep track 173 * of them yourself. g_task_attach_source() simplifies the case 174 * of waiting for a source to fire (automatically using the correct 175 * #GMainContext and priority). 176 * 177 * Here is an example for chained asynchronous operations: 178 * |[<!-- language="C" --> 179 * typedef struct { 180 * Cake *cake; 181 * CakeFrostingType frosting; 182 * char *message; 183 * } BakingData; 184 * 185 * static void 186 * decoration_data_free (BakingData *bd) 187 * { 188 * if (bd->cake) 189 * g_object_unref (bd->cake); 190 * g_free (bd->message); 191 * g_slice_free (BakingData, bd); 192 * } 193 * 194 * static void 195 * decorated_cb (Cake *cake, 196 * GAsyncResult *result, 197 * gpointer user_data) 198 * { 199 * GTask *task = user_data; 200 * GError *error = NULL; 201 * 202 * if (!cake_decorate_finish (cake, result, &error)) 203 * { 204 * g_object_unref (cake); 205 * g_task_return_error (task, error); 206 * g_object_unref (task); 207 * return; 208 * } 209 * 210 * // baking_data_free() will drop its ref on the cake, so we have to 211 * // take another here to give to the caller. 212 * g_task_return_pointer (task, g_object_ref (cake), g_object_unref); 213 * g_object_unref (task); 214 * } 215 * 216 * static gboolean 217 * decorator_ready (gpointer user_data) 218 * { 219 * GTask *task = user_data; 220 * BakingData *bd = g_task_get_task_data (task); 221 * 222 * cake_decorate_async (bd->cake, bd->frosting, bd->message, 223 * g_task_get_cancellable (task), 224 * decorated_cb, task); 225 * 226 * return G_SOURCE_REMOVE; 227 * } 228 * 229 * static void 230 * baked_cb (Cake *cake, 231 * gpointer user_data) 232 * { 233 * GTask *task = user_data; 234 * BakingData *bd = g_task_get_task_data (task); 235 * GError *error = NULL; 236 * 237 * if (cake == NULL) 238 * { 239 * g_task_return_new_error (task, BAKER_ERROR, BAKER_ERROR_NO_FLOUR, 240 * "Go to the supermarket"); 241 * g_object_unref (task); 242 * return; 243 * } 244 * 245 * bd->cake = cake; 246 * 247 * // Bail out now if the user has already cancelled 248 * if (g_task_return_error_if_cancelled (task)) 249 * { 250 * g_object_unref (task); 251 * return; 252 * } 253 * 254 * if (cake_decorator_available (cake)) 255 * decorator_ready (task); 256 * else 257 * { 258 * GSource *source; 259 * 260 * source = cake_decorator_wait_source_new (cake); 261 * // Attach @source to @task's GMainContext and have it call 262 * // decorator_ready() when it is ready. 263 * g_task_attach_source (task, source, decorator_ready); 264 * g_source_unref (source); 265 * } 266 * } 267 * 268 * void 269 * baker_bake_cake_async (Baker *self, 270 * guint radius, 271 * CakeFlavor flavor, 272 * CakeFrostingType frosting, 273 * const char *message, 274 * gint priority, 275 * GCancellable *cancellable, 276 * GAsyncReadyCallback callback, 277 * gpointer user_data) 278 * { 279 * GTask *task; 280 * BakingData *bd; 281 * 282 * task = g_task_new (self, cancellable, callback, user_data); 283 * g_task_set_priority (task, priority); 284 * 285 * bd = g_slice_new0 (BakingData); 286 * bd->frosting = frosting; 287 * bd->message = g_strdup (message); 288 * g_task_set_task_data (task, bd, (GDestroyNotify) baking_data_free); 289 * 290 * _baker_begin_cake (self, radius, flavor, cancellable, baked_cb, task); 291 * } 292 * 293 * Cake * 294 * baker_bake_cake_finish (Baker *self, 295 * GAsyncResult *result, 296 * GError **error) 297 * { 298 * g_return_val_if_fail (g_task_is_valid (result, self), NULL); 299 * 300 * return g_task_propagate_pointer (G_TASK (result), error); 301 * } 302 * ]| 303 * 304 * ## Asynchronous operations from synchronous ones 305 * 306 * You can use g_task_run_in_thread() to turn a synchronous 307 * operation into an asynchronous one, by running it in a thread. 308 * When it completes, the result will be dispatched to the 309 * [thread-default main context][g-main-context-push-thread-default] 310 * where the #GTask was created. 311 * 312 * Running a task in a thread: 313 * |[<!-- language="C" --> 314 * typedef struct { 315 * guint radius; 316 * CakeFlavor flavor; 317 * CakeFrostingType frosting; 318 * char *message; 319 * } CakeData; 320 * 321 * static void 322 * cake_data_free (CakeData *cake_data) 323 * { 324 * g_free (cake_data->message); 325 * g_slice_free (CakeData, cake_data); 326 * } 327 * 328 * static void 329 * bake_cake_thread (GTask *task, 330 * gpointer source_object, 331 * gpointer task_data, 332 * GCancellable *cancellable) 333 * { 334 * Baker *self = source_object; 335 * CakeData *cake_data = task_data; 336 * Cake *cake; 337 * GError *error = NULL; 338 * 339 * cake = bake_cake (baker, cake_data->radius, cake_data->flavor, 340 * cake_data->frosting, cake_data->message, 341 * cancellable, &error); 342 * if (cake) 343 * g_task_return_pointer (task, cake, g_object_unref); 344 * else 345 * g_task_return_error (task, error); 346 * } 347 * 348 * void 349 * baker_bake_cake_async (Baker *self, 350 * guint radius, 351 * CakeFlavor flavor, 352 * CakeFrostingType frosting, 353 * const char *message, 354 * GCancellable *cancellable, 355 * GAsyncReadyCallback callback, 356 * gpointer user_data) 357 * { 358 * CakeData *cake_data; 359 * GTask *task; 360 * 361 * cake_data = g_slice_new (CakeData); 362 * cake_data->radius = radius; 363 * cake_data->flavor = flavor; 364 * cake_data->frosting = frosting; 365 * cake_data->message = g_strdup (message); 366 * task = g_task_new (self, cancellable, callback, user_data); 367 * g_task_set_task_data (task, cake_data, (GDestroyNotify) cake_data_free); 368 * g_task_run_in_thread (task, bake_cake_thread); 369 * g_object_unref (task); 370 * } 371 * 372 * Cake * 373 * baker_bake_cake_finish (Baker *self, 374 * GAsyncResult *result, 375 * GError **error) 376 * { 377 * g_return_val_if_fail (g_task_is_valid (result, self), NULL); 378 * 379 * return g_task_propagate_pointer (G_TASK (result), error); 380 * } 381 * ]| 382 * 383 * ## Adding cancellability to uncancellable tasks 384 * 385 * Finally, g_task_run_in_thread() and g_task_run_in_thread_sync() 386 * can be used to turn an uncancellable operation into a 387 * cancellable one. If you call g_task_set_return_on_cancel(), 388 * passing %TRUE, then if the task's #GCancellable is cancelled, 389 * it will return control back to the caller immediately, while 390 * allowing the task thread to continue running in the background 391 * (and simply discarding its result when it finally does finish). 392 * Provided that the task thread is careful about how it uses 393 * locks and other externally-visible resources, this allows you 394 * to make "GLib-friendly" asynchronous and cancellable 395 * synchronous variants of blocking APIs. 396 * 397 * Cancelling a task: 398 * |[<!-- language="C" --> 399 * static void 400 * bake_cake_thread (GTask *task, 401 * gpointer source_object, 402 * gpointer task_data, 403 * GCancellable *cancellable) 404 * { 405 * Baker *self = source_object; 406 * CakeData *cake_data = task_data; 407 * Cake *cake; 408 * GError *error = NULL; 409 * 410 * cake = bake_cake (baker, cake_data->radius, cake_data->flavor, 411 * cake_data->frosting, cake_data->message, 412 * &error); 413 * if (error) 414 * { 415 * g_task_return_error (task, error); 416 * return; 417 * } 418 * 419 * // If the task has already been cancelled, then we don't want to add 420 * // the cake to the cake cache. Likewise, we don't want to have the 421 * // task get cancelled in the middle of updating the cache. 422 * // g_task_set_return_on_cancel() will return %TRUE here if it managed 423 * // to disable return-on-cancel, or %FALSE if the task was cancelled 424 * // before it could. 425 * if (g_task_set_return_on_cancel (task, FALSE)) 426 * { 427 * // If the caller cancels at this point, their 428 * // GAsyncReadyCallback won't be invoked until we return, 429 * // so we don't have to worry that this code will run at 430 * // the same time as that code does. But if there were 431 * // other functions that might look at the cake cache, 432 * // then we'd probably need a GMutex here as well. 433 * baker_add_cake_to_cache (baker, cake); 434 * g_task_return_pointer (task, cake, g_object_unref); 435 * } 436 * } 437 * 438 * void 439 * baker_bake_cake_async (Baker *self, 440 * guint radius, 441 * CakeFlavor flavor, 442 * CakeFrostingType frosting, 443 * const char *message, 444 * GCancellable *cancellable, 445 * GAsyncReadyCallback callback, 446 * gpointer user_data) 447 * { 448 * CakeData *cake_data; 449 * GTask *task; 450 * 451 * cake_data = g_slice_new (CakeData); 452 * 453 * ... 454 * 455 * task = g_task_new (self, cancellable, callback, user_data); 456 * g_task_set_task_data (task, cake_data, (GDestroyNotify) cake_data_free); 457 * g_task_set_return_on_cancel (task, TRUE); 458 * g_task_run_in_thread (task, bake_cake_thread); 459 * } 460 * 461 * Cake * 462 * baker_bake_cake_sync (Baker *self, 463 * guint radius, 464 * CakeFlavor flavor, 465 * CakeFrostingType frosting, 466 * const char *message, 467 * GCancellable *cancellable, 468 * GError **error) 469 * { 470 * CakeData *cake_data; 471 * GTask *task; 472 * Cake *cake; 473 * 474 * cake_data = g_slice_new (CakeData); 475 * 476 * ... 477 * 478 * task = g_task_new (self, cancellable, NULL, NULL); 479 * g_task_set_task_data (task, cake_data, (GDestroyNotify) cake_data_free); 480 * g_task_set_return_on_cancel (task, TRUE); 481 * g_task_run_in_thread_sync (task, bake_cake_thread); 482 * 483 * cake = g_task_propagate_pointer (task, error); 484 * g_object_unref (task); 485 * return cake; 486 * } 487 * ]| 488 * 489 * ## Porting from GSimpleAsyncResult 490 * 491 * #GTask's API attempts to be simpler than #GSimpleAsyncResult's 492 * in several ways: 493 * - You can save task-specific data with g_task_set_task_data(), and 494 * retrieve it later with g_task_get_task_data(). This replaces the 495 * abuse of g_simple_async_result_set_op_res_gpointer() for the same 496 * purpose with #GSimpleAsyncResult. 497 * - In addition to the task data, #GTask also keeps track of the 498 * [priority][io-priority], #GCancellable, and 499 * #GMainContext associated with the task, so tasks that consist of 500 * a chain of simpler asynchronous operations will have easy access 501 * to those values when starting each sub-task. 502 * - g_task_return_error_if_cancelled() provides simplified 503 * handling for cancellation. In addition, cancellation 504 * overrides any other #GTask return value by default, like 505 * #GSimpleAsyncResult does when 506 * g_simple_async_result_set_check_cancellable() is called. 507 * (You can use g_task_set_check_cancellable() to turn off that 508 * behavior.) On the other hand, g_task_run_in_thread() 509 * guarantees that it will always run your 510 * `task_func`, even if the task's #GCancellable 511 * is already cancelled before the task gets a chance to run; 512 * you can start your `task_func` with a 513 * g_task_return_error_if_cancelled() check if you need the 514 * old behavior. 515 * - The "return" methods (eg, g_task_return_pointer()) 516 * automatically cause the task to be "completed" as well, and 517 * there is no need to worry about the "complete" vs "complete 518 * in idle" distinction. (#GTask automatically figures out 519 * whether the task's callback can be invoked directly, or 520 * if it needs to be sent to another #GMainContext, or delayed 521 * until the next iteration of the current #GMainContext.) 522 * - The "finish" functions for #GTask based operations are generally 523 * much simpler than #GSimpleAsyncResult ones, normally consisting 524 * of only a single call to g_task_propagate_pointer() or the like. 525 * Since g_task_propagate_pointer() "steals" the return value from 526 * the #GTask, it is not necessary to juggle pointers around to 527 * prevent it from being freed twice. 528 * - With #GSimpleAsyncResult, it was common to call 529 * g_simple_async_result_propagate_error() from the 530 * `_finish()` wrapper function, and have 531 * virtual method implementations only deal with successful 532 * returns. This behavior is deprecated, because it makes it 533 * difficult for a subclass to chain to a parent class's async 534 * methods. Instead, the wrapper function should just be a 535 * simple wrapper, and the virtual method should call an 536 * appropriate `g_task_propagate_` function. 537 * Note that wrapper methods can now use 538 * g_async_result_legacy_propagate_error() to do old-style 539 * #GSimpleAsyncResult error-returning behavior, and 540 * g_async_result_is_tagged() to check if a result is tagged as 541 * having come from the `_async()` wrapper 542 * function (for "short-circuit" results, such as when passing 543 * 0 to g_input_stream_read_async()). 544 */ 545 public class Task : ObjectG, AsyncResultIF 546 { 547 /** the main Gtk struct */ 548 protected GTask* gTask; 549 550 /** Get the main Gtk struct */ 551 public GTask* getTaskStruct(bool transferOwnership = false) 552 { 553 if (transferOwnership) 554 ownedRef = false; 555 return gTask; 556 } 557 558 /** the main Gtk struct as a void* */ 559 protected override void* getStruct() 560 { 561 return cast(void*)gTask; 562 } 563 564 /** 565 * Sets our main struct and passes it to the parent class. 566 */ 567 public this (GTask* gTask, bool ownedRef = false) 568 { 569 this.gTask = gTask; 570 super(cast(GObject*)gTask, ownedRef); 571 } 572 573 // add the AsyncResult capabilities 574 mixin AsyncResultT!(GTask); 575 576 577 /** */ 578 public static GType getType() 579 { 580 return g_task_get_type(); 581 } 582 583 /** 584 * Creates a #GTask acting on @source_object, which will eventually be 585 * used to invoke @callback in the current 586 * [thread-default main context][g-main-context-push-thread-default]. 587 * 588 * Call this in the "start" method of your asynchronous method, and 589 * pass the #GTask around throughout the asynchronous operation. You 590 * can use g_task_set_task_data() to attach task-specific data to the 591 * object, which you can retrieve later via g_task_get_task_data(). 592 * 593 * By default, if @cancellable is cancelled, then the return value of 594 * the task will always be %G_IO_ERROR_CANCELLED, even if the task had 595 * already completed before the cancellation. This allows for 596 * simplified handling in cases where cancellation may imply that 597 * other objects that the task depends on have been destroyed. If you 598 * do not want this behavior, you can use 599 * g_task_set_check_cancellable() to change it. 600 * 601 * Params: 602 * sourceObject = the #GObject that owns 603 * this task, or %NULL. 604 * cancellable = optional #GCancellable object, %NULL to ignore. 605 * callback = a #GAsyncReadyCallback. 606 * callbackData = user data passed to @callback. 607 * 608 * Returns: a #GTask. 609 * 610 * Since: 2.36 611 * 612 * Throws: ConstructionException GTK+ fails to create the object. 613 */ 614 public this(ObjectG sourceObject, Cancellable cancellable, GAsyncReadyCallback callback, void* callbackData) 615 { 616 auto __p = g_task_new((sourceObject is null) ? null : sourceObject.getObjectGStruct(), (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, callbackData); 617 618 if(__p is null) 619 { 620 throw new ConstructionException("null returned by new"); 621 } 622 623 this(cast(GTask*) __p, true); 624 } 625 626 /** 627 * Checks that @result is a #GTask, and that @source_object is its 628 * source object (or that @source_object is %NULL and @result has no 629 * source object). This can be used in g_return_if_fail() checks. 630 * 631 * Params: 632 * result = A #GAsyncResult 633 * sourceObject = the source object 634 * expected to be associated with the task 635 * 636 * Returns: %TRUE if @result and @source_object are valid, %FALSE 637 * if not 638 * 639 * Since: 2.36 640 */ 641 public static bool isValid(AsyncResultIF result, ObjectG sourceObject) 642 { 643 return g_task_is_valid((result is null) ? null : result.getAsyncResultStruct(), (sourceObject is null) ? null : sourceObject.getObjectGStruct()) != 0; 644 } 645 646 /** 647 * Creates a #GTask and then immediately calls g_task_return_error() 648 * on it. Use this in the wrapper function of an asynchronous method 649 * when you want to avoid even calling the virtual method. You can 650 * then use g_async_result_is_tagged() in the finish method wrapper to 651 * check if the result there is tagged as having been created by the 652 * wrapper method, and deal with it appropriately if so. 653 * 654 * See also g_task_report_new_error(). 655 * 656 * Params: 657 * sourceObject = the #GObject that owns 658 * this task, or %NULL. 659 * callback = a #GAsyncReadyCallback. 660 * callbackData = user data passed to @callback. 661 * sourceTag = an opaque pointer indicating the source of this task 662 * error = error to report 663 * 664 * Since: 2.36 665 */ 666 public static void reportError(ObjectG sourceObject, GAsyncReadyCallback callback, void* callbackData, void* sourceTag, ErrorG error) 667 { 668 g_task_report_error((sourceObject is null) ? null : sourceObject.getObjectGStruct(), callback, callbackData, sourceTag, (error is null) ? null : error.getErrorGStruct(true)); 669 } 670 671 /** 672 * A utility function for dealing with async operations where you need 673 * to wait for a #GSource to trigger. Attaches @source to @task's 674 * #GMainContext with @task's [priority][io-priority], and sets @source's 675 * callback to @callback, with @task as the callback's `user_data`. 676 * 677 * It will set the @source’s name to the task’s name (as set with 678 * g_task_set_name()), if one has been set. 679 * 680 * This takes a reference on @task until @source is destroyed. 681 * 682 * Params: 683 * source = the source to attach 684 * callback = the callback to invoke when @source triggers 685 * 686 * Since: 2.36 687 */ 688 public void attachSource(Source source, GSourceFunc callback) 689 { 690 g_task_attach_source(gTask, (source is null) ? null : source.getSourceStruct(), callback); 691 } 692 693 /** 694 * Gets @task's #GCancellable 695 * 696 * Returns: @task's #GCancellable 697 * 698 * Since: 2.36 699 */ 700 public Cancellable getCancellable() 701 { 702 auto __p = g_task_get_cancellable(gTask); 703 704 if(__p is null) 705 { 706 return null; 707 } 708 709 return ObjectG.getDObject!(Cancellable)(cast(GCancellable*) __p); 710 } 711 712 /** 713 * Gets @task's check-cancellable flag. See 714 * g_task_set_check_cancellable() for more details. 715 * 716 * Since: 2.36 717 */ 718 public bool getCheckCancellable() 719 { 720 return g_task_get_check_cancellable(gTask) != 0; 721 } 722 723 /** 724 * Gets the value of #GTask:completed. This changes from %FALSE to %TRUE after 725 * the task’s callback is invoked, and will return %FALSE if called from inside 726 * the callback. 727 * 728 * Returns: %TRUE if the task has completed, %FALSE otherwise. 729 * 730 * Since: 2.44 731 */ 732 public bool getCompleted() 733 { 734 return g_task_get_completed(gTask) != 0; 735 } 736 737 /** 738 * Gets the #GMainContext that @task will return its result in (that 739 * is, the context that was the 740 * [thread-default main context][g-main-context-push-thread-default] 741 * at the point when @task was created). 742 * 743 * This will always return a non-%NULL value, even if the task's 744 * context is the default #GMainContext. 745 * 746 * Returns: @task's #GMainContext 747 * 748 * Since: 2.36 749 */ 750 public MainContext getContext() 751 { 752 auto __p = g_task_get_context(gTask); 753 754 if(__p is null) 755 { 756 return null; 757 } 758 759 return new MainContext(cast(GMainContext*) __p); 760 } 761 762 /** 763 * Gets @task’s name. See g_task_set_name(). 764 * 765 * Returns: @task’s name, or %NULL 766 * 767 * Since: 2.60 768 */ 769 public string getName() 770 { 771 return Str.toString(g_task_get_name(gTask)); 772 } 773 774 /** 775 * Gets @task's priority 776 * 777 * Returns: @task's priority 778 * 779 * Since: 2.36 780 */ 781 public int getPriority() 782 { 783 return g_task_get_priority(gTask); 784 } 785 786 /** 787 * Gets @task's return-on-cancel flag. See 788 * g_task_set_return_on_cancel() for more details. 789 * 790 * Since: 2.36 791 */ 792 public bool getReturnOnCancel() 793 { 794 return g_task_get_return_on_cancel(gTask) != 0; 795 } 796 797 /** 798 * Gets the source object from @task. Like 799 * g_async_result_get_source_object(), but does not ref the object. 800 * 801 * Returns: @task's source object, or %NULL 802 * 803 * Since: 2.36 804 */ 805 public ObjectG getSourceObject() 806 { 807 auto __p = g_task_get_source_object(gTask); 808 809 if(__p is null) 810 { 811 return null; 812 } 813 814 return ObjectG.getDObject!(ObjectG)(cast(GObject*) __p); 815 } 816 817 /** 818 * Gets @task's source tag. See g_task_set_source_tag(). 819 * 820 * Returns: @task's source tag 821 * 822 * Since: 2.36 823 */ 824 public void* getSourceTag() 825 { 826 return g_task_get_source_tag(gTask); 827 } 828 829 /** 830 * Gets @task's `task_data`. 831 * 832 * Returns: @task's `task_data`. 833 * 834 * Since: 2.36 835 */ 836 public void* getTaskData() 837 { 838 return g_task_get_task_data(gTask); 839 } 840 841 /** 842 * Tests if @task resulted in an error. 843 * 844 * Returns: %TRUE if the task resulted in an error, %FALSE otherwise. 845 * 846 * Since: 2.36 847 */ 848 public bool hadError() 849 { 850 return g_task_had_error(gTask) != 0; 851 } 852 853 /** 854 * Gets the result of @task as a #gboolean. 855 * 856 * If the task resulted in an error, or was cancelled, then this will 857 * instead return %FALSE and set @error. 858 * 859 * Since this method transfers ownership of the return value (or 860 * error) to the caller, you may only call it once. 861 * 862 * Returns: the task result, or %FALSE on error 863 * 864 * Since: 2.36 865 * 866 * Throws: GException on failure. 867 */ 868 public bool propagateBoolean() 869 { 870 GError* err = null; 871 872 auto __p = g_task_propagate_boolean(gTask, &err) != 0; 873 874 if (err !is null) 875 { 876 throw new GException( new ErrorG(err) ); 877 } 878 879 return __p; 880 } 881 882 /** 883 * Gets the result of @task as an integer (#gssize). 884 * 885 * If the task resulted in an error, or was cancelled, then this will 886 * instead return -1 and set @error. 887 * 888 * Since this method transfers ownership of the return value (or 889 * error) to the caller, you may only call it once. 890 * 891 * Returns: the task result, or -1 on error 892 * 893 * Since: 2.36 894 * 895 * Throws: GException on failure. 896 */ 897 public ptrdiff_t propagateInt() 898 { 899 GError* err = null; 900 901 auto __p = g_task_propagate_int(gTask, &err); 902 903 if (err !is null) 904 { 905 throw new GException( new ErrorG(err) ); 906 } 907 908 return __p; 909 } 910 911 /** 912 * Gets the result of @task as a pointer, and transfers ownership 913 * of that value to the caller. 914 * 915 * If the task resulted in an error, or was cancelled, then this will 916 * instead return %NULL and set @error. 917 * 918 * Since this method transfers ownership of the return value (or 919 * error) to the caller, you may only call it once. 920 * 921 * Returns: the task result, or %NULL on error 922 * 923 * Since: 2.36 924 * 925 * Throws: GException on failure. 926 */ 927 public void* propagatePointer() 928 { 929 GError* err = null; 930 931 auto __p = g_task_propagate_pointer(gTask, &err); 932 933 if (err !is null) 934 { 935 throw new GException( new ErrorG(err) ); 936 } 937 938 return __p; 939 } 940 941 /** 942 * Gets the result of @task as a #GValue, and transfers ownership of 943 * that value to the caller. As with g_task_return_value(), this is 944 * a generic low-level method; g_task_propagate_pointer() and the like 945 * will usually be more useful for C code. 946 * 947 * If the task resulted in an error, or was cancelled, then this will 948 * instead set @error and return %FALSE. 949 * 950 * Since this method transfers ownership of the return value (or 951 * error) to the caller, you may only call it once. 952 * 953 * Params: 954 * value = return location for the #GValue 955 * 956 * Returns: %TRUE if @task succeeded, %FALSE on error. 957 * 958 * Since: 2.64 959 * 960 * Throws: GException on failure. 961 */ 962 public bool propagateValue(out Value value) 963 { 964 GValue* outvalue = sliceNew!GValue(); 965 GError* err = null; 966 967 auto __p = g_task_propagate_value(gTask, outvalue, &err) != 0; 968 969 if (err !is null) 970 { 971 throw new GException( new ErrorG(err) ); 972 } 973 974 value = ObjectG.getDObject!(Value)(outvalue, true); 975 976 return __p; 977 } 978 979 /** 980 * Sets @task's result to @result and completes the task (see 981 * g_task_return_pointer() for more discussion of exactly what this 982 * means). 983 * 984 * Params: 985 * result = the #gboolean result of a task function. 986 * 987 * Since: 2.36 988 */ 989 public void returnBoolean(bool result) 990 { 991 g_task_return_boolean(gTask, result); 992 } 993 994 /** 995 * Sets @task's result to @error (which @task assumes ownership of) 996 * and completes the task (see g_task_return_pointer() for more 997 * discussion of exactly what this means). 998 * 999 * Note that since the task takes ownership of @error, and since the 1000 * task may be completed before returning from g_task_return_error(), 1001 * you cannot assume that @error is still valid after calling this. 1002 * Call g_error_copy() on the error if you need to keep a local copy 1003 * as well. 1004 * 1005 * See also g_task_return_new_error(). 1006 * 1007 * Params: 1008 * error = the #GError result of a task function. 1009 * 1010 * Since: 2.36 1011 */ 1012 public void returnError(ErrorG error) 1013 { 1014 g_task_return_error(gTask, (error is null) ? null : error.getErrorGStruct(true)); 1015 } 1016 1017 /** 1018 * Checks if @task's #GCancellable has been cancelled, and if so, sets 1019 * @task's error accordingly and completes the task (see 1020 * g_task_return_pointer() for more discussion of exactly what this 1021 * means). 1022 * 1023 * Returns: %TRUE if @task has been cancelled, %FALSE if not 1024 * 1025 * Since: 2.36 1026 */ 1027 public bool returnErrorIfCancelled() 1028 { 1029 return g_task_return_error_if_cancelled(gTask) != 0; 1030 } 1031 1032 /** 1033 * Sets @task's result to @result and completes the task (see 1034 * g_task_return_pointer() for more discussion of exactly what this 1035 * means). 1036 * 1037 * Params: 1038 * result = the integer (#gssize) result of a task function. 1039 * 1040 * Since: 2.36 1041 */ 1042 public void returnInt(ptrdiff_t result) 1043 { 1044 g_task_return_int(gTask, result); 1045 } 1046 1047 /** 1048 * Sets @task's result to @result and completes the task. If @result 1049 * is not %NULL, then @result_destroy will be used to free @result if 1050 * the caller does not take ownership of it with 1051 * g_task_propagate_pointer(). 1052 * 1053 * "Completes the task" means that for an ordinary asynchronous task 1054 * it will either invoke the task's callback, or else queue that 1055 * callback to be invoked in the proper #GMainContext, or in the next 1056 * iteration of the current #GMainContext. For a task run via 1057 * g_task_run_in_thread() or g_task_run_in_thread_sync(), calling this 1058 * method will save @result to be returned to the caller later, but 1059 * the task will not actually be completed until the #GTaskThreadFunc 1060 * exits. 1061 * 1062 * Note that since the task may be completed before returning from 1063 * g_task_return_pointer(), you cannot assume that @result is still 1064 * valid after calling this, unless you are still holding another 1065 * reference on it. 1066 * 1067 * Params: 1068 * result = the pointer result of a task 1069 * function 1070 * resultDestroy = a #GDestroyNotify function. 1071 * 1072 * Since: 2.36 1073 */ 1074 public void returnPointer(void* result, GDestroyNotify resultDestroy) 1075 { 1076 g_task_return_pointer(gTask, result, resultDestroy); 1077 } 1078 1079 /** 1080 * Sets @task's result to @result (by copying it) and completes the task. 1081 * 1082 * If @result is %NULL then a #GValue of type %G_TYPE_POINTER 1083 * with a value of %NULL will be used for the result. 1084 * 1085 * This is a very generic low-level method intended primarily for use 1086 * by language bindings; for C code, g_task_return_pointer() and the 1087 * like will normally be much easier to use. 1088 * 1089 * Params: 1090 * result = the #GValue result of 1091 * a task function 1092 * 1093 * Since: 2.64 1094 */ 1095 public void returnValue(Value result) 1096 { 1097 g_task_return_value(gTask, (result is null) ? null : result.getValueStruct()); 1098 } 1099 1100 /** 1101 * Runs @task_func in another thread. When @task_func returns, @task's 1102 * #GAsyncReadyCallback will be invoked in @task's #GMainContext. 1103 * 1104 * This takes a ref on @task until the task completes. 1105 * 1106 * See #GTaskThreadFunc for more details about how @task_func is handled. 1107 * 1108 * Although GLib currently rate-limits the tasks queued via 1109 * g_task_run_in_thread(), you should not assume that it will always 1110 * do this. If you have a very large number of tasks to run (several tens of 1111 * tasks), but don't want them to all run at once, you should only queue a 1112 * limited number of them (around ten) at a time. 1113 * 1114 * Params: 1115 * taskFunc = a #GTaskThreadFunc 1116 * 1117 * Since: 2.36 1118 */ 1119 public void runInThread(GTaskThreadFunc taskFunc) 1120 { 1121 g_task_run_in_thread(gTask, taskFunc); 1122 } 1123 1124 /** 1125 * Runs @task_func in another thread, and waits for it to return or be 1126 * cancelled. You can use g_task_propagate_pointer(), etc, afterward 1127 * to get the result of @task_func. 1128 * 1129 * See #GTaskThreadFunc for more details about how @task_func is handled. 1130 * 1131 * Normally this is used with tasks created with a %NULL 1132 * `callback`, but note that even if the task does 1133 * have a callback, it will not be invoked when @task_func returns. 1134 * #GTask:completed will be set to %TRUE just before this function returns. 1135 * 1136 * Although GLib currently rate-limits the tasks queued via 1137 * g_task_run_in_thread_sync(), you should not assume that it will 1138 * always do this. If you have a very large number of tasks to run, 1139 * but don't want them to all run at once, you should only queue a 1140 * limited number of them at a time. 1141 * 1142 * Params: 1143 * taskFunc = a #GTaskThreadFunc 1144 * 1145 * Since: 2.36 1146 */ 1147 public void runInThreadSync(GTaskThreadFunc taskFunc) 1148 { 1149 g_task_run_in_thread_sync(gTask, taskFunc); 1150 } 1151 1152 /** 1153 * Sets or clears @task's check-cancellable flag. If this is %TRUE 1154 * (the default), then g_task_propagate_pointer(), etc, and 1155 * g_task_had_error() will check the task's #GCancellable first, and 1156 * if it has been cancelled, then they will consider the task to have 1157 * returned an "Operation was cancelled" error 1158 * (%G_IO_ERROR_CANCELLED), regardless of any other error or return 1159 * value the task may have had. 1160 * 1161 * If @check_cancellable is %FALSE, then the #GTask will not check the 1162 * cancellable itself, and it is up to @task's owner to do this (eg, 1163 * via g_task_return_error_if_cancelled()). 1164 * 1165 * If you are using g_task_set_return_on_cancel() as well, then 1166 * you must leave check-cancellable set %TRUE. 1167 * 1168 * Params: 1169 * checkCancellable = whether #GTask will check the state of 1170 * its #GCancellable for you. 1171 * 1172 * Since: 2.36 1173 */ 1174 public void setCheckCancellable(bool checkCancellable) 1175 { 1176 g_task_set_check_cancellable(gTask, checkCancellable); 1177 } 1178 1179 /** 1180 * Sets @task’s name, used in debugging and profiling. The name defaults to 1181 * %NULL. 1182 * 1183 * The task name should describe in a human readable way what the task does. 1184 * For example, ‘Open file’ or ‘Connect to network host’. It is used to set the 1185 * name of the #GSource used for idle completion of the task. 1186 * 1187 * This function may only be called before the @task is first used in a thread 1188 * other than the one it was constructed in. It is called automatically by 1189 * g_task_set_source_tag() if not called already. 1190 * 1191 * Params: 1192 * name = a human readable name for the task, or %NULL to unset it 1193 * 1194 * Since: 2.60 1195 */ 1196 public void setName(string name) 1197 { 1198 g_task_set_name(gTask, Str.toStringz(name)); 1199 } 1200 1201 /** 1202 * Sets @task's priority. If you do not call this, it will default to 1203 * %G_PRIORITY_DEFAULT. 1204 * 1205 * This will affect the priority of #GSources created with 1206 * g_task_attach_source() and the scheduling of tasks run in threads, 1207 * and can also be explicitly retrieved later via 1208 * g_task_get_priority(). 1209 * 1210 * Params: 1211 * priority = the [priority][io-priority] of the request 1212 * 1213 * Since: 2.36 1214 */ 1215 public void setPriority(int priority) 1216 { 1217 g_task_set_priority(gTask, priority); 1218 } 1219 1220 /** 1221 * Sets or clears @task's return-on-cancel flag. This is only 1222 * meaningful for tasks run via g_task_run_in_thread() or 1223 * g_task_run_in_thread_sync(). 1224 * 1225 * If @return_on_cancel is %TRUE, then cancelling @task's 1226 * #GCancellable will immediately cause it to return, as though the 1227 * task's #GTaskThreadFunc had called 1228 * g_task_return_error_if_cancelled() and then returned. 1229 * 1230 * This allows you to create a cancellable wrapper around an 1231 * uninterruptible function. The #GTaskThreadFunc just needs to be 1232 * careful that it does not modify any externally-visible state after 1233 * it has been cancelled. To do that, the thread should call 1234 * g_task_set_return_on_cancel() again to (atomically) set 1235 * return-on-cancel %FALSE before making externally-visible changes; 1236 * if the task gets cancelled before the return-on-cancel flag could 1237 * be changed, g_task_set_return_on_cancel() will indicate this by 1238 * returning %FALSE. 1239 * 1240 * You can disable and re-enable this flag multiple times if you wish. 1241 * If the task's #GCancellable is cancelled while return-on-cancel is 1242 * %FALSE, then calling g_task_set_return_on_cancel() to set it %TRUE 1243 * again will cause the task to be cancelled at that point. 1244 * 1245 * If the task's #GCancellable is already cancelled before you call 1246 * g_task_run_in_thread()/g_task_run_in_thread_sync(), then the 1247 * #GTaskThreadFunc will still be run (for consistency), but the task 1248 * will also be completed right away. 1249 * 1250 * Params: 1251 * returnOnCancel = whether the task returns automatically when 1252 * it is cancelled. 1253 * 1254 * Returns: %TRUE if @task's return-on-cancel flag was changed to 1255 * match @return_on_cancel. %FALSE if @task has already been 1256 * cancelled. 1257 * 1258 * Since: 2.36 1259 */ 1260 public bool setReturnOnCancel(bool returnOnCancel) 1261 { 1262 return g_task_set_return_on_cancel(gTask, returnOnCancel) != 0; 1263 } 1264 1265 /** 1266 * Sets @task's source tag. 1267 * 1268 * You can use this to tag a task return 1269 * value with a particular pointer (usually a pointer to the function 1270 * doing the tagging) and then later check it using 1271 * g_task_get_source_tag() (or g_async_result_is_tagged()) in the 1272 * task's "finish" function, to figure out if the response came from a 1273 * particular place. 1274 * 1275 * A macro wrapper around this function will automatically set the 1276 * task’s name to the string form of @source_tag if it’s not already 1277 * set, for convenience. 1278 * 1279 * Params: 1280 * sourceTag = an opaque pointer indicating the source of this task 1281 * 1282 * Since: 2.36 1283 */ 1284 public void setSourceTag(void* sourceTag) 1285 { 1286 g_task_set_source_tag(gTask, sourceTag); 1287 } 1288 1289 /** 1290 * Sets @task's task data (freeing the existing task data, if any). 1291 * 1292 * Params: 1293 * taskData = task-specific data 1294 * taskDataDestroy = #GDestroyNotify for @task_data 1295 * 1296 * Since: 2.36 1297 */ 1298 public void setTaskData(void* taskData, GDestroyNotify taskDataDestroy) 1299 { 1300 g_task_set_task_data(gTask, taskData, taskDataDestroy); 1301 } 1302 }